package TetrisFlash.Screen 
{
	import starling.display.Image;
	import starling.events.KeyboardEvent;
	import TetrisFlash.Game;
	import TetrisFlash.InGameManager.ManagerDrawGame;
	import TetrisFlash.InGameManager.ManagerRunGame;
	import TetrisFlash.Objects.TetrisBoard.TetrisBoard;
	import TetrisFlash.Objects.Tetrominoes.ITetromino;
	import TetrisFlash.Objects.Tetrominoes.TetrominoI;
	import TetrisFlash.Objects.Tetrominoes.TetrominoL1;
	import TetrisFlash.Objects.Tetrominoes.TetrominoL2;
	import TetrisFlash.Objects.Tetrominoes.TetrominoO;
	import TetrisFlash.Objects.Tetrominoes.TetrominoT;
	import TetrisFlash.GameLoadData.AssetsTexture;
	import TetrisFlash.Objects.Tetrominoes.TetrominoZ1;
	import TetrisFlash.Objects.Tetrominoes.TetrominoZ2;
	import TetrisFlash.Utilities.RandomNumberHelper;
	import TetrisFlash.GameLoadData.SoundManager;
	/**
	 * ...
	 * @author DANTRUONG
	 */
	public class MainGameScreen extends Screen 
	{
		public var bgImage:Image;
		public var managerRunGame:ManagerRunGame;
		public var managerDrawGame:ManagerDrawGame;
		
		public function MainGameScreen(_game: Game) 
		{
			super(_game);
		}
		
		override public function initScreen():void {
			bgImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("background"));
			addChild(bgImage);
			
			managerRunGame = new ManagerRunGame(this);
			managerDrawGame = new ManagerDrawGame(this);
			
			myGame.soundManager.playBackSoundGame();
			
			this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function onKeyDown(e:KeyboardEvent):void 
		{
			if (!managerDrawGame.isEffect) {
				switch(e.keyCode) {
					case 37://Left arrow
						managerRunGame.tetrominoTetris.isMoveLeftOK(managerRunGame.boardTetris);
						break;
					case 38://Up arrow
						managerRunGame.tetrominoTetris.isRotation(managerRunGame.boardTetris);
						break;
					case 39://Right arrow
						managerRunGame.tetrominoTetris.isMoveRightOK(managerRunGame.boardTetris);
						break;
					case 40://Down arrow
						if (managerRunGame.tetrominoTetris.isMoveDownOK(managerRunGame.boardTetris)) {
							managerRunGame.curSpeedDown = 0;
						}
						break;
					case 32://Spacebar
						if (!managerRunGame.isHold) {
							var tmp:int = managerRunGame.holdTetromino;
							if (tmp == 0) {
								tmp = managerRunGame.nextTetromino;
								managerRunGame.nextTetromino = RandomNumberHelper.randomIntRange(1, 7);
								managerRunGame.isHold = false;
							}else {
								managerRunGame.isHold = true;
							}
							managerRunGame.holdTetromino = managerRunGame.tetrominoTetris.tetrominoID;
							//remove quadbatch of cur tetromino from screen
							managerRunGame.tetrominoTetris.release();
							
							var tetroTmp:ITetromino;
							switch(tmp) {
								case 1:
									tetroTmp = new TetrominoI(this);
									break;
								case 2:
									tetroTmp = new TetrominoL1(this);
									break;
								case 3:
									tetroTmp = new TetrominoL2(this);
									break;
								case 4:
									tetroTmp = new TetrominoO(this);
									break;
								case 5:
									tetroTmp = new TetrominoT(this);
									break
								case 6:
									tetroTmp = new TetrominoZ1(this);
									break;
								case 7:
									tetroTmp = new TetrominoZ2(this);
									break;
							}
							tetroTmp.init();
							managerRunGame.tetrominoTetris = tetroTmp;
						}
						break;
				}
			}
		}
		
		override public function updateScreen(deltaTime: Number):void {
			if (!managerDrawGame.isEffect) {
				//Auto down
				if (managerRunGame.curSpeedDown < managerRunGame.speedDown) {
					managerRunGame.curSpeedDown++;
				}else {
					managerRunGame.curSpeedDown = 0;
					if (!managerRunGame.tetrominoTetris.isMoveDownOK(managerRunGame.boardTetris)) {
						//add tetromino to board
						managerRunGame.boardTetris.addTetromino(managerRunGame.tetrominoTetris);
						myGame.soundManager.play(SoundManager.SOUND_ADDBRICK);
						
						//...
						if (managerRunGame.boardTetris.countFullLines()) {
							managerDrawGame.isEffect = true;
						}
						//...
						if (managerRunGame.boardTetris.isFull()) {
							trace("GAMEOVER");
						}
						
						var tetroTmp:ITetromino;
						switch(managerRunGame.nextTetromino) {
								case 1:
									tetroTmp = new TetrominoI(this);
									break;
								case 2:
									tetroTmp = new TetrominoL1(this);
									break;
								case 3:
									tetroTmp = new TetrominoL2(this);
									break;
								case 4:
									tetroTmp = new TetrominoO(this);
									break;
								case 5:
									tetroTmp = new TetrominoT(this);
									break
								case 6:
									tetroTmp = new TetrominoZ1(this);
									break;
								case 7:
									tetroTmp = new TetrominoZ2(this);
									break;
						}
						tetroTmp.init();
						managerRunGame.tetrominoTetris = tetroTmp;
						managerRunGame.isHold = false;
						managerRunGame.nextTetromino = RandomNumberHelper.randomIntRange(1, 7);	
					}
				}
			}
			else {
				//update effect clear
				managerDrawGame.update();
				if (!managerDrawGame.isEffect) {
					managerRunGame.boardTetris.clearLines();
				}
			}
		}
		
		override public function drawScreen():void {
			managerDrawGame.updateDrawBoardWithColor(managerRunGame.boardTetris);
			managerDrawGame.drawTetromino(managerRunGame.tetrominoTetris);
			
			managerDrawGame.drawTetrominoHoldXY(managerRunGame.holdTetromino, 32, 100);
			managerDrawGame.drawTetrominoNextXY(managerRunGame.nextTetromino, 414, 100);
		}
		
		override public function destroyScreen():void {
			managerDrawGame.ReleaseManagerDrawGame();
			managerRunGame.ReleaseManagerRunGame();
			bgImage.dispose();
		}			

	}

}